
namespace SB2.Core;
public static class PermissionCode
{
    const string LinkStr = "_";
    #region 分组
    [Permission(Name = "系统管理", Sort = "0", PermissionType = PermissionType.Group)]
    public const string GSys = "G0";
    [Permission(Name = "权限管理", Sort = "0", PermissionType = PermissionType.Group, ParentPermission = GSys)]
    public const string GPermission = GSys + LinkStr + "0";
    [Permission(Name = "人事管理", Sort = "1", PermissionType = PermissionType.Group)]
    public const string GPerson = "G1";
    #endregion

    #region 页面
    [Permission(Name = "角色", Sort = "0", PermissionType = PermissionType.Page, ParentPermission = GPermission)]
    public const string PRole = "1";
    [Permission(Name = "用户", Sort = "1", PermissionType = PermissionType.Page, ParentPermission = GPermission)]
    public const string PUser = "2";
    [Permission(Name = "用户组", Sort = "01", PermissionType = PermissionType.Page, ParentPermission = GPermission)]
    public const string PUserGroup = "3";
    #endregion

    [Permission(Name = "查询", Sort = "0", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleShow = PRole + LinkStr + "0";
    [Permission(Name = "新增", Sort = "00", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleAdd = PRole + LinkStr + "1";
    [Permission(Name = "删除", Sort = "1", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleDelete = PRole + LinkStr + "2";
    [Permission(Name = "查看用户", Sort = "2", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleShowInfo = PRole + LinkStr + "3";
    [Permission(Name = "功能权限（web）", Sort = "3", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleWeb = PRole + LinkStr + "4";
    [Permission(Name = "功能权限（移动端）", Sort = "4", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleMobile = PRole + LinkStr + "5";
    [Permission(Name = "修改", Sort = "5", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleUpdate = PRole + LinkStr + "6";
    [Permission(Name = "数据报表权限", Sort = "6", PermissionType = PermissionType.Button, ParentPermission = PRole)]
    public const string RoleReport = PRole + LinkStr + "7";
    public static Dictionary<string, PermissionAttribute> GetCodes()
    {
        var types = typeof(PermissionCode);
        var datas = types.GetFields()
        .ToDictionary(m => m.GetRawConstantValue() + "",
        m => m.GetCustomAttributes(typeof(PermissionAttribute), false)
        .Select(m => (PermissionAttribute)m).FirstOrDefault());
        return datas;
    }

    public static List<string> GetApis()
    {
        var permissionDic = GetCodes();
        return permissionDic.Values
        .Where(m => !string.IsNullOrEmpty(m.Apis))
        .SelectMany(m => (m.Apis + "").Split(','))
        .ToList();
    }
}